#include "TargetDummy.h"
#include "Factory.h"
#include "ReportError.h"
#include "TextureManager.h"
#include "PlayState.h"
#include "StringUtil.h"
#include "Screen.h"
#include "SoundManager.h"
#include "PlayState.h"
#include "Catapult.h"
#include <assert.h>
#include <gl\glut.h>



TargetDummy::TargetDummy() 
{
	TheEventPoller::Instance()->AddHandler(this);
	m_mat.SetIdentity();
}


void TargetDummy::BTCollided(GameObject* go, const btVector3& ang, const btVector3& lin)
{

	if(go->GetTypeName() == "TargetDummy")
	{
		if(ThePlayState::Instance()->GetCatapultAddress()->GetIsWaiting())
		{
			static float f = 0;
			f+= 0.1f;


			if(ang.length2() > 0.4f || lin.length() > 0.4f)
			{
				static float f= 0;
				f += 0.1f;
				if(f >= 1)
				{
					//Do stuff!
					//ThePlayState::Instance()->AddToScore(1);
					//TheSoundManager::Instance()->SoundEffectPlay(1);
				}
			}
		}
	}
}



bool TargetDummy::Load(File* const f)
{
	//Format is: 
	// Texture path
	// xyz pos
	// xyz size
	// mass
	// damping

	//Texture path
	if(!f->GetString(&m_texPath))
	{
		ReportError("Ball texture not found");
		assert(0);
	}

	// Position
	if(!f->GetFloat(&m_pos.X))
	{
		ReportError("TargetDummy xPos missing ID:" + m_id + __LINE__);
		assert(0);
	}
	if(!f->GetFloat(&m_pos.Y))
	{
		ReportError("TargetDummy yPos missing ID:" + m_id + __LINE__);
		assert(0);
	}
	if(!f->GetFloat(&m_pos.Z))
	{
		ReportError("TargetDummy zPos missing ID:" + m_id + __LINE__);
		assert(0);
	}
	
	// XYZ sizes
	float xSize, ySize, zSize;
	if(!f->GetFloat(&xSize))
	{
		ReportError("TargetDummy xSize missing ID:" + m_id + __LINE__);
		assert(0);
	}
	if(!f->GetFloat(&ySize))
	{
		ReportError("TargetDummy ySize missing ID:" + m_id + __LINE__);
		assert(0);
	}
	if(!f->GetFloat(&zSize))
	{
		ReportError("TargetDummy zSize missing ID:" + m_id + __LINE__);
		assert(0);
	}

	//Mass
	if(!f->GetFloat(&m_mass))
	{
		ReportError("BallMass"+__LINE__);
		assert(0);
	}
	float damping;
	if(!f->GetFloat(&damping))
	{
		ReportError("Can't find ball dampening factor");
		assert(0);
	}
	m_cube = GLCuboid(xSize, ySize, zSize);
	//int r = xSize*ySize*zSize;
	m_mass = xSize*ySize*zSize * 0.00001;

	//Collision Box Shape takes half the length
	CreateCollisionShape(new btBoxShape(btVector3(xSize, ySize, zSize)), m_mass, COL_TARGET, targetCollidesWith);
	mp_rigidBody->setDamping(damping, damping);
	ResetPosition();

	return true;
}

const char* TargetDummy::GetTypeName()
{
	return "TargetDummy";
}

GameObject* CreateTargetDummy()
{
	return new TargetDummy;
}

void TargetDummy::Draw()
{
		glPushMatrix();

			btTransform btTrans;
			mp_motionState->getWorldTransform(btTrans);
			btTrans.getOpenGLMatrix(m_mat.m_matrix);
			m_mat.MultiOpenGL();

			TheTextureManager::Instance()->DrawTexture(m_texPath);
			m_cube.Draw();
			
		glPopMatrix();
}



void TargetDummy::ResetPosition()
{
	//Uses a btTransform to set the position of the block back its starting point
	btTransform bt;
	btQuaternion quat;

	m_id;
	bt.setIdentity();
	bt.setRotation(quat);
	bt.setOrigin(btVector3(m_pos.X, m_pos.Y, m_pos.Z));

	mp_rigidBody->setWorldTransform(bt);
	mp_rigidBody->setLinearVelocity(btVector3(0,0,0));
	mp_rigidBody->setAngularVelocity(btVector3(0,0,0));
}

void TargetDummy::Update()
{

	mp_rigidBody->activate(true);

}

void TargetDummy::OnKeyboardEvent(const SDL_KeyboardEvent& k)
{
	if(k.keysym.sym == SDLK_y && k.state == SDL_PRESSED)
	{
		ResetPosition();
	}
}

TargetDummy::~TargetDummy()
{
	TheEventPoller::Instance()->RemoveHandler(this);
	delete mp_rigidBody;
	delete mp_motionState;
}

static bool b = TheGameObjectFactory::Instance()->AddToMap("TargetDummy", CreateTargetDummy);